package com.jy.adscm.operation.newhscrb.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.bean.CgSupplier;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgSupplierMapper;
import com.jy.adscm.operation.mask.bean.KzMaterial;
import com.jy.adscm.operation.newhscrb.bean.NhcMaterial;
import com.jy.adscm.operation.newhscrb.bean.NhcMaterialDetail;
import com.jy.adscm.operation.newhscrb.bean.QueryNhcMaterial;
import com.jy.adscm.operation.newhscrb.mapper.mysql.NhcMaterialMapper;
import com.jy.adscm.operation.newhscrb.service.NhcMaterialDetailService;
import com.jy.adscm.operation.newhscrb.service.NhcMaterialService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-06-15
 */
@Service
public class NhcMaterialServiceImpl extends ServiceImpl<NhcMaterialMapper, NhcMaterial> implements NhcMaterialService {
     @Autowired
    JyUserMapper  jyUserMapper;
     @Autowired
     CgMaterialMapper cgMaterialMapper;
     @Autowired
     CgSupplierMapper cgSupplierMapper;
     @Autowired
    NhcMaterialDetailService nhcMaterialDetailService;
    @Override
    public boolean insertNhcMaterial(NhcMaterial nhcMaterial) {
        //生成二维码信息 年+月+日 +序号
        String date =new SimpleDateFormat("yyyyMMdd").format(new Date());
        Wrapper<NhcMaterial> wrapper=new EntityWrapper<>();
        wrapper.like("code",date, SqlLike.RIGHT);
        wrapper.orderBy("code",false);
        List<NhcMaterial> nhcMaterials = super.selectList(wrapper);
        String code="";
        if (nhcMaterials!=null&&nhcMaterials.size()>0){
            NhcMaterial machine = nhcMaterials.get(0);
            code = String.valueOf(Long.valueOf(machine.getCode())+1) ;
        }else {
            code=date+"0001";
        }
        nhcMaterial.setCode(code);
        boolean insert = super.insert(nhcMaterial);
        return insert;
    }

    @Override
    public Page<NhcMaterial> select(QueryNhcMaterial queryNhcMaterial) {
        Page<NhcMaterial> page=new Page<>(queryNhcMaterial.getCurrentPage(),queryNhcMaterial.getPageSize());
        Wrapper<NhcMaterial> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryNhcMaterial.getBatch(),null)){
            wrapper.like("batch",queryNhcMaterial.getBatch());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryNhcMaterial.getCode(),null)){
            wrapper.like("code",queryNhcMaterial.getCode());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryNhcMaterial.getStartTime(),null)){
            wrapper.ge("in_stock_date",queryNhcMaterial.getStartTime());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryNhcMaterial.getEndTime(),null)){
            wrapper.le("in_stock_date",queryNhcMaterial.getEndTime());
        }
        wrapper.orderBy("create_time",false);
        page=super.selectPage(page,wrapper);
        List<NhcMaterial> nhcMaterials = page.getRecords();
        for (NhcMaterial nhcMaterial : nhcMaterials) {
            loadNhcMaterial(nhcMaterial);
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    private void loadNhcMaterial(NhcMaterial nhcMaterial) {
        if(AdscmBaseUtils.getInstance().isNotEmpty(nhcMaterial.getMaterial(),null)){
            CgMaterial cgMaterial = cgMaterialMapper.selectById(nhcMaterial.getMaterial());
            nhcMaterial.setCgMaterial(cgMaterial);
        }
        if(AdscmBaseUtils.getInstance().isNotEmpty(nhcMaterial.getSupplier(),null)){
            CgSupplier cgSupplier = cgSupplierMapper.selectById(nhcMaterial.getSupplier());
            nhcMaterial.setCgSupplier(cgSupplier);
        }
        if(AdscmBaseUtils.getInstance().isNotEmpty(nhcMaterial.getInStocker(),null)){
            JyUser jyUser = jyUserMapper.selectById(nhcMaterial.getInStocker());
            nhcMaterial.setInStockerName(jyUser.getName());
        }
        
    }

    @Override
    public NhcMaterialDetail selectByCode(String code) {
        NhcMaterialDetail result =new NhcMaterialDetail();
        Wrapper<NhcMaterialDetail> wrapper=new EntityWrapper<>();
        wrapper.eq("code",code);
        List<NhcMaterialDetail> nhcMaterialDetails = nhcMaterialDetailService.selectList(wrapper);
        if (nhcMaterialDetails!=null &&nhcMaterialDetails.size()>0){
            result = nhcMaterialDetails.get(0);
            Long pid = result.getPid();
            NhcMaterial nhcMaterial = super.selectById(pid);
            loadNhcMaterial(nhcMaterial);
            result.setNhcMaterial(nhcMaterial);
            if (AdscmBaseUtils.getInstance().isNotEmpty(result.getOutStocker(),null)){
                JyUser jyUser = jyUserMapper.selectById(result.getOutStocker());
                result.setOutStockerName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(result.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(result.getCreator());
                result.setCreatorName(jyUser.getName());
            }
        }
        return result;
    }
}
